<?php
// @formatter:off
/**
 * @file s.rules.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: s.rules.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/services/s.rules.php $
 */
// @formatter:on

$wcServiceStatus = false;
$wcServiceErrorCode = 500;

if(defined('__SERVICE__') && $wcGroup && !$wcGroupForbidden && $wcProfile->allowedAny(WC_PERM_RULE_ADD, WC_PERM_RULE_EDIT, WC_PERM_RULE_LIST, WC_PERM_RULE_RENAME, WC_PERM_RULE_REMOVE)) {
	/*
	 * Conditions:
	 *
	 * Request Method GET:
	 *	- When there's a rule id specified ($wcRule) and the
	 *	  requesting user is allowed to list rules, the result is
	 *	  the list of rule-actions belonging to the specified one.
	 *	- When there's no rule id specified ($wcRule) and the
	 *	  requesting user is allowed to list rules, the result is
	 *	  the full list of rules.
	 *	- In other case, it returns an parameters error message.
	 *
	 * Request Method POST:
	 *	- When there's the parameter "create" ($_REQUEST), the
	 *	  requesting user is allowed to add a rule and a parameter
	 *	  called NAME is set in post data, the service will attempt to
	 *	  create a new rule. As a result, tag "wc-result" will contain the
	 *	  new rule's name and id.
	 *	- When there's the parameter "rename" ($_REQUEST), there's a
	 *	  rule specified ($wcRule), the requesting user is allowed to
	 *	  rename rules and a parameter called NEXT_NAME is set in post
	 *	  data, the service will attempt to rename the rule with the new
	 *	  name.
	 *	- When there's a rule id specified ($wcRule), the requesting user
	 *	  is allowed to remove rules and a parameter called "remove" is
	 *	  set in GET data, the service will attempt to delete the rule.
	 *	- In other case, it returns an parameters error message.
	 */
	if($_SERVER["REQUEST_METHOD"] == "GET") {
		if($wcRule && $wcProfile->allowedTo(WC_PERM_RULE_LIST)) {
			//
			// Rule's actions list requested.
			$wcServiceStatus = true;

			$wcServiceXMLResult .= "<rule>";
			$wcServiceXMLResult .= "<id>{$wcRule->id}</id>";
			$wcServiceXMLResult .= "<name>{$wcRule->name}</name>";
			$wcServiceXMLResult .= "<rule-actions>";

			$items = $wcRule->actions();
			uasort($items, "WCRuleAction::Sort");
			foreach($items as $action) {
				$wcServiceXMLResult .= "<rule-action>";
				$wcServiceXMLResult .= "<id>{$action->id()}</id>";
				$wcServiceXMLResult .= "<name>{$action->name()}</name>";
				$wcServiceXMLResult .= "<type>{$action->type()}</type>";
				if($action->mode) {
					$wcServiceXMLResult .= "<mode>{$action->mode}</mode>";
				}
				$wcServiceXMLResult .= "</rule-action>";
			}
			$wcServiceXMLResult .= "</rule-actions>";
			$wcServiceXMLResult .= "</rule>";
		} elseif($wcProfile->allowedTo(WC_PERM_RULE_LIST)) {
			//
			// Full rules list requested.
			global $wcGroup;
			global $wcRulesHolder;

			$wcServiceStatus = true;

			foreach($wcRulesHolder->items($wcGroup) as $rule) {
				$wcServiceXMLResult .= "<rule>";
				$wcServiceXMLResult .= "<id>{$rule->id}</id>";
				$wcServiceXMLResult .= "<name>{$rule->name}</name>";
				$wcServiceXMLResult .= "<rule-actions>";

				$items = $rule->actions();
				uasort($items, "WCRuleAction::Sort");
				foreach($items as $action) {
					$wcServiceXMLResult .= "<rule-action>";
					$wcServiceXMLResult .= "<id>{$action->id()}</id>";
					$wcServiceXMLResult .= "<name>{$action->name()}</name>";
					$wcServiceXMLResult .= "<type>{$action->type()}</type>";
					if($action->mode) {
						$wcServiceXMLResult .= "<mode>{$action->mode}</mode>";
					}
					$wcServiceXMLResult .= "</rule-action>";
				}
				$wcServiceXMLResult .= "</rule-actions>";
				$wcServiceXMLResult .= "</rule>";
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} elseif($_SERVER["REQUEST_METHOD"] == "POST") {
		if(isset($_REQUEST["create"]) && isset($_POST["NAME"]) && strlen(trim($_POST["NAME"])) && $wcProfile->allowedTo(WC_PERM_RULE_ADD)) {
			$name = trim($_POST["NAME"]);

			$rule = @$wcRulesHolder->create($name, $wcGroup);
			if($rule) {
				$wcServiceStatus = true;
				$wcServiceXMLResult .= "<rule>";
				$wcServiceXMLResult .= "<id>{$rule->id}</id>";
				$wcServiceXMLResult .= "<name>{$rule->name}</name>";
				$wcServiceXMLResult .= "<rule-actions></rule-actions>";
				$wcServiceXMLResult .= "</rule>";
			} else {
				$wcServiceErrorMsg = "Unable to create rule '{$name}'";
			}
		} elseif(isset($_REQUEST["rename"]) && isset($_POST["NEXT_NAME"]) && strlen(trim($_POST["NEXT_NAME"])) && $wcProfile->allowedTo(WC_PERM_RULE_RENAME)) {
			if($wcRule && $wcRule->ok()) {
				$next = trim($_POST["NEXT_NAME"]);
				$name = $wcRule->name;

				if(!$next) {
					$wcServiceErrorMsg = "No new name specified";
				} elseif($next == $name) {
					$wcServiceErrorMsg = "New name is the same than current one";
				} else {
					$wcServiceStatus = true;
					$wcRule->name = $next;
				}
			} else {
				$wcServiceErrorMsg = "This service was call with invalid parameters";
			}
		} elseif($wcRule && isset($_REQUEST["remove"]) && $wcProfile->allowedTo(WC_PERM_RULE_REMOVE)) {
			if($wcRule->remove()) {
				$wcServiceStatus = true;
			} else {
				$wcServiceErrorMsg = "Unable to remove rule '{$name}'";
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	}
} else {
	$wcServiceErrorCode = 403;
	$wcServiceErrorMsg = "Unable to access service";
}
?>